﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
    <link href="~/Areas/Back/lib/layui/css/layui.css" rel="stylesheet" />
    <link href="~/Areas/Back/lib/layui/css/layui.mobile.css" rel="stylesheet" />
    <script src="~/Areas/Back/lib/layui/layui.js"></script>
</head>
<body>
    <div>
        <div class="layui-container" style="padding-top:20px;margin-bottom:100px;">
            @*onsubmit="return false" 禁止form表单提交*@
            <form class="layui-form" id="addForm" onsubmit="return false">
                @*标题*@
                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" id="Title" class="layui-input" placeholder="请输入标题" />
                    </div>            
                </div>            
                @* 新闻详情 *@
                <div class="layui-form-item">
                    <label class="layui-form-label">新闻详情</label>
                    <div class="layui-input-block">
                        <textarea id="Cont" lay-filter="content" style="display:none;"></textarea>
                    </div>
                </div>           
                @*类别*@
                <div class="layui-form-item">
                    <label class="layui-form-label">类别</label>
                    <div class="layui-input-block">
                        @*<input type="text" id="Type" class="layui-input" placeholder="请输入类别" />*@
                        <select id="TypeId">
                            <option value="">请选择</option>
                            @foreach (var item in ViewBag.A)
                            {
                                <option value="@item.Id">@item.Name</option>
                            }

                        </select>
                    </div>
                </div>
                @*是否显示在首页*@
                <div class="layui-form-item">
                    <label class="layui-form-label">是否显示在首页</label>
                    <div class="layui-input-block">
                        <select id="HomeFlag">
                            <option value="">请选择</option>
                            <option value="是">是</option>
                            <option value="否">否</option>
                        </select>
                    </div>
                </div>
                @*提交和重置按钮*@
                <div class="layui-form-item">

                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="subAdd">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</body>
<script>
    layui.use(['form', 'layedit','upload','layer'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var layedit = layui.layedit;
        var upload = layui.upload;
        var $ = layui.$;
        //富文本上传图片
        layedit.set({
            uploadImage: {
                url: '/Back/News/UploadLayedit2', //上传图片的接口url
                type: 'post' //默认post
            }
        });
        var index = layedit.build('Cont');//富文本编辑器
        form.verify({
            content: function () {
                layedit.sync(index);
            }
        });
        //图片上传
        upload.render({
            elem: "#uplaodImg",
            url: "UploadPhoto",//图片上传地址
            done: function (res) {
                //上传成功
                if (res.success == true) {
                    //存到input 里，好传值
                    $("#ImgUrl").val(res.path);
                    $("#imgPhoto").attr("src", "/" + res.path);
                    layer.msg("上传成功");
                } else {
                    layer.msg("上传失败:" + res.message);
                }
            }
        });
        //开始点击提交
        form.on('submit(subAdd)', function () {
            //ajax请求
            $.ajax({
                url: "AddDel",
                method: "post",
                dataType: "json",
                data: {
                    Title: $("#Title").val(),              
                    Cont: layedit.getContent(index),
                    TypeId: $("#TypeId").val(),
                    HomeFlag: $("#HomeFlag").val(),
                },
                success: function (res) {
                    if (res.success == true) {
                        layer.msg(res.msg);
                        //1.获取弹窗的编号
                        var index = parent.layer.getFrameIndex(window.name);
                        //2.关闭窗口
                        parent.layer.close(index);
                        //3.刷新父级别页面
                        parent.location.reload();
                    } else {
                        layer.msg(res.msg);
                    }
                }
            })
            return false;
        })
    });
</script>
</html>
